package com.shxc.smarttrade.old.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.adapter.MarketInformationTwoAdapter;
import com.shxc.smarttrade.old.bean.MarketMsgBean;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.myinterface.MarketInformationClickListener;
import com.shxc.smarttrade.old.myview.MyFootView;
import com.shxc.smarttrade.old.myview.MyRefreshHeadView;
import com.shxc.smarttrade.old.myview.RecycleViewDivider;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;
import com.shxc.smarttrade.old.utils.SpUtils;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/7/27.
 */

public class MarketMsgListActivity extends BaseActivity {
    @BindView(R.id.tv_center)
    TextView tvCenter;
    @BindView(R.id.x_recycler_view)
    XRecyclerView xRecyclerView;
    private LinearLayoutManager manager;
    private MarketInformationTwoAdapter adapter;
    private int page = 1;
    private List<MarketMsgBean.DataBean> allDB = new ArrayList<>();

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    MarketMsgBean msb = (MarketMsgBean) msg.obj;
                    if (msb.getErr_code().equals("0")) {
                        List<MarketMsgBean.DataBean> db = msb.getData();
                        if (db.size() != 0) {
                            allDB.addAll(db);
                            adapter.setDatas(db);
                        } else {
                            xRecyclerView.loadMoreComplete();
                            showToast("已加载全部!");
                        }
                    } else {
                        showToast(msb.getErr_msg());
                    }
                    xRecyclerView.refreshComplete();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_position_history);
        initView();
        initListener();
        getMarketMsgList(page, 10);
    }

    private void initListener() {
        adapter.setMarketInforItemCLickListener(new MarketInformationClickListener() {
            @Override
            public void onItemCLick(int position, MarketMsgBean.DataBean msb) {
                Intent inttent = new Intent(MarketMsgListActivity.this, MarketDetailActivity.class);
                inttent.putExtra("msgId", msb.getId());
                startActivity(inttent);
            }

            @Override
            public void onMoreClick(int position) {

            }
        });

        xRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xRecyclerView.refreshComplete();
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        getMarketMsgList(page, 10);
                    }
                }, 1000);
            }
        });
    }

    private void initView() {
        tvCenter.setText("行情速递");
        manager = new LinearLayoutManager(this);
        xRecyclerView.setLayoutManager(manager);
        xRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayout.VERTICAL, 2, getResources().getColor(R.color.dividing_line_color)));
        xRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallSpinFadeLoader);
        MyRefreshHeadView myRefreshHeadView = new MyRefreshHeadView(this);
        myRefreshHeadView.setArrowImageView(R.mipmap.indicator_arrow);
        xRecyclerView.setRefreshHeader(myRefreshHeadView);
        xRecyclerView.setFootView(new MyFootView(this));
        xRecyclerView.setAdapter(adapter = new MarketInformationTwoAdapter(this));
    }

    @OnClick(R.id.rl_left)
    public void onViewClicked() {
        finish();
    }


    private void getMarketMsgList(int page, int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("accessToken", SpUtils.getString(this, "accessToken", ""));
        map.put("page", page + "");
        map.put("pageSize", pageSize + "");
        MyOkHttpUtils.getServiceDatas(this,map, URLContents.HOME_FRAGMENT_REAL_MSG, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                showToast("网络异常");
            }

            @Override
            public void success(String response, int id) {
                Gson gson = new Gson();
                MarketMsgBean msb = gson.fromJson(response, MarketMsgBean.class);
                Message msg = Message.obtain();
                msg.what = 0;
                msg.obj = msb;
                handler.sendMessage(msg);
            }
        });
    }
}
